package klimuts.periodicals.catalog.actions;
import java.util.ArrayList;
import java.util.Map;

import klimuts.periodicals.catalog.Catalog;
import klimuts.periodicals.catalog.Periodical;
import klimuts.periodicals.user.Cart;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Action add selected periodicals to user cart
 * @author Siarhei Klimuts
 */
public class AddToCart extends ActionSupport implements SessionAware{
    private Map<String,Object> session;
    private long id;
    
	public String execute() throws Exception {
		Cart cart=(Cart)session.get("cart");
		Catalog catalog=(Catalog)session.get("catalog");
		if (catalog!=null){
			Periodical periodical=catalog.getPeriodical(id);
			if (periodical!=null) {
				if (cart==null) {
					cart=new Cart();
					session.put("cart", cart);
				}
				cart.add(periodical);
			}
		}
		return "success";
	}
	
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
